#ifndef _TCPCONNECTION_H
#define _TCPCONNECTION_H

#include "SocketIO.hh"
#include "Socket.hh"
#include "InetAddress.hh"
#include <sys/socket.h>
#include <string>
#include <functional>
#include <memory>

using std::function;
using std::shared_ptr;
using std::enable_shared_from_this;



class TcpConnection
    :public enable_shared_from_this<TcpConnection>
{
    using fun_ptr = function<void(const shared_ptr<TcpConnection>&)>;
public:
    explicit TcpConnection(int fd);
    ~TcpConnection();

    InetAddress Get_localaddr();
    InetAddress Get_peeraddr();

    void Send(const string &msg);
    string Recv();
    string To_string();
    bool Is_close();

    void Set_new_conn(const fun_ptr& new_conn);
    void Set_old_conn(const fun_ptr& old_conn);
    void Set_close_conn(const fun_ptr& close_conn);

    void New_conn();
    void Old_conn();
    void Close_conn();

    TcpConnection(const TcpConnection &) = delete;
    TcpConnection& operator=(const TcpConnection& rhs) = delete;
private:
    SocketIO _sock_io;
    Socket _sock; // 这个是新连接的文件描述符
    InetAddress _localAddr; // 本地的信息
    InetAddress _peerAddr; // 其他连接的信息
    fun_ptr _new_conn;
    fun_ptr _old_conn;
    fun_ptr _close_conn;
};

#endif